layui.config({
    base: "/layui/"
}).extend({
    index: 'lib/index'
}).use(['index', 'form', 'jquery', 'table'], function () {
    var form = layui.form,
        table = layui.table,
        admin = layui.admin;

    /**
     * 防重复设置
     * @type {boolean}
     */
    var edit_flag = true;


    form.verify({
        FinalRate: function (value) {

            if (value < 0 || value > 0.6) {
                return '微信结算费率设置不合理';
            }
        },
        SubsidyRate: function (value) {
            if (value < 0 || value > 0.2) {
                //小于0.2
                return '补贴设置不合理';
            }
        },
        qrLimit: function (value) {
            if (value < 0 ) {
                //小于0.2
                return '必须大于0';
            }else if (!/^\d+(\.\d{1,2})?$/.test(value)) {
                return '最多输入两位小数';
            }

        }
    });


    table.render({
        elem: '#mch_pay_info_table'
        , url: '/mch_pay_info/queryData'
        , method: "post"
        , height: 500
        , limit: 50							//显示数目
        , limits: [50, 100]
        , cols: [[

            {field: 'userId', width: 100, title: '用户ID', "align": "center"},
            {field: 'userName', width: 150, title: '用户名', "align": "center"},
            {field: 'trueName', width: 100, title: '真实名', "align": "center"}
            , {field: 'mobile', width: 150, title: '手机号', "align": "center"}
            , {field: 'wxSubMchId', width: 120, title: '微信商户号', "align": "center"}
            , {field: 'wxSubAppId', width: 120, title: '微信公众号', "align": "center"}
            , {field: 'wxFinalRate', width: 100, title: '微信费率', "align": "center"}
            , {field: 'aliUserId', width: 150, title: '支付宝用户ID', "align": "center"}
            , {field: 'wxSubsidyRate', width: 150, title: '微信补贴', "align": "center"}
            , {field: 'aliFinalRate', width: 150, title: '支付宝官方费率', "align": "center"}
            , {field: 'aliSubsidyRate', width: 150, title: '支付宝结算价', "align": "center"}
            , {
                field: 'sqrStatus', width: 150, title: '收钱码功能', "align": "center", templet: function (d) {
                    if (d.sqrStatus == 1) {
                        return "开启";
                    } else {
                        return "关闭"
                    }
                }
            }
            , {
                field: 'sqrLimit', width: 160, title: '收钱码金额限制/元', "align": "center", templet: function (d) {
                    if (d.sqrLimit == null || d.sqrLimit == 0) {
                        return "无限制";
                    } else {
                        return new Number(d.sqrLimit.toString())/(new Number(100));
                    }
                }
            }
            , {field: 'createdTime', width: 200, title: '创建时间', "align": "center"}
            , {field: 'OPT', width: 200, title: '操作', "align": "center", toolbar: "#OptData"}
        ]]
        , request: {
            pageName: 'pageNo' //页码的参数名称，默认：page
            , limitName: 'pageSize' //每页数据量的参数名，默认：limit
        },
        where: {
            userName: $(".queryTable input[name='userName']").val(),
            mobile: $(".queryTable input[name='mobile']").val()
        }
        , id: 'mch_pay_info_table'
        , page: true
    });

    table.on('tool(mch_pay_info_table)', function (obj) {

        var data = obj.data;

        $("#edit_mch_payinfo_form input[name='mobile']").attr("disabled", "true");
        $("#edit_mch_payinfo_form input[name='userName']").attr("disabled", "true");

        $("#edit_mch_payinfo_form")[0].reset();
        form.render();
        edit_flag = true;

        form.val('edit_mch_payinfo_form', {
            "id": data.id,
            "userName": data.userName,
            "mobile": data.mobile,
            "wxSubMchId": data.wxSubMchId,
            "aliUserId": data.aliUserId,
            "wxSubAppId": data.wxSubAppId,
            "wxFinalRate": data.wxFinalRate,
            "wxSubsidyRate": data.wxSubsidyRate,
            "aliFinalRate": data.aliFinalRate,
            "aliSubsidyRate": data.aliSubsidyRate,
            "sqrStatus": data.sqrStatus,
            "sqrLimit": data.sqrLimit/100
        });


        if(data.sqrStatus==1){
            $("#close").removeAttr("checked");
            $("#start").attr("checked", "checked");
        }else if(data.sqrStatus==0) {
            $("#start").removeAttr("checked");
            $("#close").attr("checked", "checked");

        }

        form.render();


        admin.popup({
            title: '编辑'
            , shade: 0
            , anim: -1
            , offset: "20px"
            , area: ['80%', '500px']
            , skin: 'layui-anim layui-anim-upbit'
            , content: $("#add_agencyUser_win")
            , end: function () {
                table.reload('mch_pay_info_table', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    , where: {}
                });
            }
        });

    });


    /**
     * 支付配置编辑提交
     */
    form.on('submit(editpayinfo)', function (data) {
        if (edit_flag) {
            edit_flag = false;
           var limit= data.field.sqrLimit;
            data.field.sqrLimit=limit*100;
            $.ajax({
                url: "/mch_pay_info/edit",
                data: data.field,
                type: "post",
                success: function (result) {
                    if (result.code == 1) {
                        edit_flag = true;
                        layer.msg("编辑成功", {
                            offset: '32%'
                            , icon: 1
                            , time: 0
                            , btn: '知道了'
                            , btnAlign: "c"
                            , shade: 0.4
                            , yes: function () {
                                table.reload('mch_pay_info_table', {
                                    where: {
                                        userName: $(".queryTable input[name='userName']").val()
                                    }
                                });
                                layer.closeAll();
                            }
                        })
                    } else {
                        edit_flag = true;
                        layer.msg(result.message, {
                            offset: '32%'
                            , icon: 2
                            , time: 3000
                        }, function () {
                            layer.closeAll();
                        })
                    }

                }
            });
        } else {
            layer.msg("不能重复提交表单", {
                offset: '32%'
                , icon: 2
                , time: 3000
            }, function () {

            })
        }

        return false;
    });


    $(".search_btn").click(function () {

        table.reload('mch_pay_info_table', {
            page: {
                curr: 1 //重新从第 1 页开始
            }
            , where: {
                userName: $(".queryTable input[name='userName']").val(),
                mobile: $(".queryTable input[name='mobile']").val()
            }
        });

    });

});



